﻿var S_Json = {}; //用于保存自定义格式的返回格式的Json 如：{ "userid": "1", "id": "1", "created": "R_Created", "deleted": "R_Deleted", "action": "06" }
var FlexiJson = {}; //用于存取列头和列内容的Json
var checkTrue = new Array(); //用于存取当前页中选中的checkbox的值。
var checkFalse = new Array(); //用于存取当前页中未选中的checkbox的值。
var R_Created = ""; //返回的结果数组，存放的是相对于原数据增加的数据
var R_Deleted = ""; //返回的结果数组，存放的是相对于原数据删除的数据。
//获取数据
function GetData(v_url, v_data, option, ControlName) {
    $.ajax({
        type: "post",
        dataType: "json",
        async: false,
        data: v_data,
        url: v_url,
        success: function (message) {
            $(".flexigrid").each(function (i) {
                $(this).attr("id", "flexigrid" + i);
            });
            FlexiJson = message;
            option.colModel = FlexiJson.colModel;
            var grid = $("#" + ControlName).flexigrid(option);
            grid.flexAddData(FlexiJson);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}
//先中页面中有权限的checkbox
function SetChecked() {
    S_Json = { "usrid": "1", "id": "1", "created": [], "deleted": [] };
    $.each(FlexiJson.rows, function (i, a) {
        if (a.checked == true) {
            S_Json.created.push(a.id);
            $("#chk_" + a.id).attr("checked", true);
            $("#row" + a.id).addClass("trSelected");
        } else {
            S_Json.deleted.push(a.id);
            $("#chk_" + a.id).attr("checked", false);
            $("#row" + a.id).removeClass("trSelected");
        }
    });
}

//保存的逻辑回发
function SavePostBack(v_url, v_data) {
    $("input:[class='itemchk']").each(function () { //遍历class="itemchk"的checkbox。
        if ($(this).is(':checked')) { //判断当前的checkbox是否选中。
            var id = $(this).val();
            checkTrue.push(id); //反选中项的值push到checkTrue;
        } else {
            var id = $(this).val();
            checkFalse.push(id);
        }
    });
    $.each(checkTrue, function (i, item) {
        if (("," + S_Json.created + ",").indexOf("," + item + ",") == -1) { //如果在原始选中的里面检索不到当前选中的信息 则把信息添加入craeted里
            R_Created = R_Created + item + ",";
        }
    });
    $.each(checkFalse, function (i, item) {
        if (("," + S_Json.deleted + ",").indexOf("," + item + ",") == -1) { //如果在原始未选中的里面检索不到当前未选中的信息 deleted
            R_Deleted = R_Deleted + item + ",";
        }
    });
    v_data.created = R_Created;
    v_data.deleted = R_Deleted;
    //把整理好的数据发送回服务器
    $.ajax({
        type: "post",
        dataType: "json",
        data: v_data,
        url: v_url,
        success: function (message) {
            ymPrompt.succeedInfo("保存成功");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            ymPrompt.alert(textStatus);
        }
    });
}